#include "loginviewmodel.h"
#include <QDebug>

LoginViewModel::LoginViewModel(QObject *parent, TcpService* tcpclient) : QObject(parent), tcpclient(tcpclient)
{
    connect(tcpclient, &TcpService::messageReceived, this, &LoginViewModel::respHandle);
}

bool LoginViewModel::loginRequest(const QString& usernumber, const QString& password)
{
    ProtocolLoginReqPtr req = std::make_shared<ProtocolLoginReq>();
    req->setUserid(usernumber);
    req->setPassword(password);
    qDebug() << "发送请求类型：" << req->getType();
    return tcpclient->sendData(req);
}

void LoginViewModel::respHandle(const ProtocolPtr &resp)
{
    // 使用dynamic_pointer_cast进行安全转换
    ProtocolCommonRespPtr commonResp = std::dynamic_pointer_cast<ProtocolCommonResp>(resp);

    if (commonResp)
    {
        // 转换成功，指针有效
        qDebug() << "Login userid: " << commonResp->getCode();
    }
    else
    {
        // 转换失败
        qDebug() << "Failed to convert to ProtocolLogin";
    }
}
